import json
import time
from apps.forum.models import ForumType,Forum
from apps.comments.models import Comment
from django.http import JsonResponse
from django.views import View
import base64
from apps.users.models import User
from django.contrib.contenttypes.models import ContentType

from apps.likes.models import LikeCount,LikeRecord
from utils.models import BaseModel


class uploadImg(View):
    def post(self,request):
        data = json.loads(request.body.decode())
        img=data.get('img')
        ines = img.split('base64,')
        imgData = base64.b64decode(ines[1])
        timestamp = str(int(time.time()))
        file_url = 'media/%s.%s' % (timestamp, 'jpg')
        leniyimg = open(file_url, 'wb')
        leniyimg.write(imgData)
        leniyimg.close()
        return JsonResponse({'ret':0,'msg':'success','img':file_url })
class gettype(View):
    def get(self,request):
        forum=ForumType.objects.all()
        info=[]
        for item in forum:
            info.append({
            'typename':item.type_name
            })
        return JsonResponse({'code':'0','errsmg':'ok','type':info})

class postarticle(View):
    def post(self,request):
        try:
            user=request.user
            json_str = request.body
            json_str.decode()
            req_data = json.loads(json_str)
            title = req_data.get('title')
            article_type1 = req_data.get('type')
            int(article_type1)
            article_type = ForumType.objects.get(id=article_type1)
            content = req_data.get('content')
            # 处理图片逻辑 第一张图
            img_first=req_data.get('pic1')
            if img_first:
                ines =img_first.split('base64,')
                imgData = base64.b64decode(ines[1])
                timestamp = str(int(time.time()))
                file_url1 = 'media/%s.%s' % (timestamp, 'jpg')
                img1=file_url1
                leniyimg = open(file_url1, 'wb')
                leniyimg.write(imgData)
                leniyimg.close()
            else:
                img1=''
            # 处理图片逻辑 第二张图
            img_second = req_data.get('pic2')
            if img_second:
                ines2 = img_second.split('base64,')
                imgData2 = base64.b64decode(ines2[1])
                timestamp = str(int(time.time())+1)
                file_url2 = 'media/%s.%s' % (timestamp, 'jpg')
                img2 = file_url2
                leniyimg = open(file_url2, 'wb')
                leniyimg.write(imgData2)
                leniyimg.close()
            else:
                img2 = ''
            # 处理图片逻辑 第3张图
            img_third = req_data.get('pic3')
            if img_third:
                ines = img_third.split('base64,')
                imgData3 = base64.b64decode(ines[1])
                timestamp = str(int(time.time())+2)
                file_url3 = 'media/%s.%s' % (timestamp, 'jpg')
                img3 = file_url3
                leniyimg = open(file_url3, 'wb')
                leniyimg.write(imgData3)
                leniyimg.close()
            else:
                img3 = ''
            forum = Forum.objects.create(
                title=title,
                blog_type=article_type,
                content=content,
                author=user,
                image1=img1,
                image2=img2,
                image3=img3
            )
            model_class = ContentType.objects.get(model='forum')
            # article_likes = LikeCount.objects.filter(content_type=model_class)
            LikeCount.objects.create(
                content_type=model_class,
                object_id=forum.id
            )
            return JsonResponse({'code': 0, 'errmsg': 'ok'})
        except:
            return JsonResponse({'code':400,'errmsg':'用户未登录'})


class contentadd(View):
    def get(self,request,article_id):
        forum=Forum.objects.get(id=article_id)
        info=[]
        info.append({
            'pic1':str(forum.image1),
            'pic2': str(forum.image2),
            'pic3': str(forum.image3),
            'title':forum.title,
            'content':forum.content,
            'create_time': forum.create_time,
            'nickname':forum.author.nickname,
            'img_header':str(forum.author.img_header),
            'username':forum.author.username
        })
        return JsonResponse({'code':0,'info':info,'read_num':forum.read_num})
class readnumadd(View):
    def post(self,request):
        json_str = request.body
        json_str.decode()
        req_data = json.loads(json_str)
        article_id=req_data.get('article_id')
        forum=Forum.objects.get(id=article_id)
        forum.read_num=forum.read_num+1
        forum.save()
        return JsonResponse({'code':0,'id':article_id,'num':forum.read_num,'errmsg':'阅读数+1'})
class getallcontent(View):
    def get(self, request):

        forum = Forum.objects.all()
        model_class=ContentType.objects.get(model='forum')

        info = []
        for item in forum:
            id=int(item.id)
            article_likes = LikeCount.objects.get(content_type=model_class,object_id=id).liked_num
            comment_num=Comment.objects.filter(content_type=model_class, object_id=id).count()
            info.append({
                'pic1': str(item.image1),
                'pic2': str(item.image2),
                'pic3': str(item.image3),
                'title': item.title,
                'content': item.content,
                'id':id,
                'create_time':item.create_time,
                'read_num':item.read_num,
                'nickname':item.author.nickname,
                'author_img':str(item.author.img_header),
                'like_num':article_likes,
               'comment_num':comment_num
             })

        return JsonResponse({'code': 0, 'info': info})

class getAllauthor(View):
    def get(self, request):
        forum = Forum.objects.all()
        info = []
        for item in forum:
            author_img = str(User.objects.get(id=item.author_id).img_header),
            author_nickname = User.objects.get(id=item.author_id).nickname
            info.append({
                'id':item.id,
                'author_img':author_img,
                'author_nickname':author_nickname

             })
        return JsonResponse({'code': 0, 'info': info})
class getTypecontent(View):
    def get(self, request):
        typeid=request.GET.get('typeid')
        forum = Forum.objects.filter(blog_type=typeid)
        model_class=ContentType.objects.get(model='forum')

        info = []
        for item in forum:
            id=int(item.id)
            article_likes = LikeCount.objects.get(content_type=model_class,object_id=id).liked_num
            comment_num=Comment.objects.filter(content_type=model_class, object_id=id).count()
            info.append({
                'pic1': str(item.image1),
                'pic2': str(item.image2),
                'pic3': str(item.image3),
                'title': item.title,
                'content': item.content,
                'id':id,
                'create_time':item.create_time,
                'read_num':item.read_num,
                'nickname':item.author.nickname,
                'author_img':str(item.author.img_header),
                'like_num':article_likes,
               'comment_num':comment_num
             })

        return JsonResponse({'code': 0, 'info': info})